package com.nine.system.controller;



import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.nine.common.entity.OrderCount;
import com.nine.common.entity.OrderFormVO101;
import com.nine.common.entity.OrderRollbackCount;
import com.nine.common.tool.ResponseResult;
import com.nine.common.entity.OrderForm;
import com.nine.system.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Date;
import java.util.List;

/**
 * @Description:
 * @ClassName: OrderController
 * @Author: luodeng
 * @Date: 2023/6/5 16:53
 * @Version: 1.0
 */
@Api(tags = "订单接口",description = "有关订单的所有接口")
@RestController
@RequestMapping("/order")
public class OrderController {


    @Autowired
    private OrderService orderService;

    @SaCheckRole("102")
    @ApiOperation(value = "商家提交订单")
    @PostMapping("/admit")
    public ResponseResult admitOrder(@ApiParam(value = "机器型号",required = true)@RequestParam("productType") String productType,
                                     @ApiParam(value = "故障描述",required = true)@RequestParam("orderDescription")String orderDescription,
                                     @ApiParam(value = "订单优先级",required = true)@RequestParam("orderPriority")int orderPriority,
                                     @ApiParam(value = "顾客姓名",required = true)@RequestParam("orderClientname")String orderClientname,
                                     @ApiParam(value = "顾客手机号",required = true)@RequestParam("orderClientphone")String orderClientphone,
                                     @ApiParam(value = "省份",required = true)@RequestParam("orderProvince")String orderProvince,
                                     @ApiParam(value = "市区",required = true)@RequestParam("orderCity")String orderCity,
                                     @ApiParam(value = "区县",required = true)@RequestParam("orderArea")String orderArea,
                                     @ApiParam(value = "详细地址",required = true)@RequestParam("orderAddress")String orderAddress,
                                     @ApiParam(value = "预约上门时间",required = true)@RequestParam("orderDate") Date orderDate,
                                     @ApiParam(value = "上门时间点",required = true)@RequestParam("orderTime")int orderTime){
        return orderService.addOrder(productType,orderDescription,orderPriority,orderClientname,orderClientphone,orderProvince,orderCity,orderArea,orderAddress,orderDate,orderTime);
    }

    @ApiOperation(value = "根据筛选条件查找订单")
    @PostMapping("/search")
    public ResponseResult searchOrders(@ApiParam(value = "省份",required = true)@RequestParam("orderProvince") String orderProvince,
                                       @ApiParam(value = "市区",required = true)@RequestParam("orderCity") String orderCity,
                                       @ApiParam(value = "区县",required = true)@RequestParam("orderArea") String orderArea,
                                       @ApiParam(value = "上门时间点",required = true)@RequestParam("orderTime") int orderTime,
                                       @RequestParam("page") int page,
                                       @RequestParam("limit") int limit) {
        return orderService.searchOrders(orderProvince, orderCity, orderArea, orderTime, page, limit);
    }


    @ApiOperation(value = "查看某个订单的所有日志信息")
    @ApiImplicitParam(value = "订单号",name = "orderId",dataType = "long",dataTypeClass = long.class,paramType = "path")
    @GetMapping("/getLogs/{orderId}")
    public ResponseResult getLogs(@PathVariable long orderId,int page, int limit){
        return orderService.getLogs(orderId, page, limit);
    }

    @ApiOperation(value = "查询所有订单")
    @GetMapping("/getAll")
    public ResponseResult<List<OrderFormVO101>> getAllOrders(int page, int limit){
        return orderService.getAll(page, limit);
    }


    @ApiOperation(value = "查找满足条件的订单")
    @PostMapping("/findSome")
    public ResponseResult findSome(
            @ApiParam(value = "订单状态",required = true)@RequestParam("orderStatus") int orderStatus,
            @ApiParam(value = "订单优先级",required = true)@RequestParam("orderPriority") int orderPriority,
            @ApiParam(value = "预约上门时间",required = true)@RequestParam("orderDate") String orderDate,
            @ApiParam(value = "上门时间点",required = true)@RequestParam("orderTime") int orderTime,
            @ApiParam(value = "订单号",required = true)@RequestParam("orderId") String orderId,
            @ApiParam(value = "顾客手机号",required = true)@RequestParam("orderClientphone") String orderClientphone,
            @RequestParam("page")int page,
            @RequestParam("limit")int limit){

        return orderService.findSome(orderStatus,orderPriority,orderDate,orderTime,orderId,orderClientphone,page,limit);
    }

    @ApiOperation(value = "根据订单号查找某个具体订单信息")
    @PostMapping("/findByOrderId")
    public ResponseResult findByOrderId(
            @ApiParam(value = "订单号",required = true)@RequestParam("orderId") String orderId,
            @RequestParam("page")int page,
            @RequestParam("limit")int limit){

        return orderService.findByOrderId(orderId,page,limit);
    }

    @ApiOperation(value = "查找所有未被接单的订单")
    @GetMapping("/getNo")
    public ResponseResult getOrders(){
        return orderService.getNo();
    }

    @ApiOperation(value = "工程师接单接口")
    @GetMapping("/takeOrder/{orderId}")
    public ResponseResult takeOrder(@ApiParam(value = "订单号",required = true)@PathVariable long orderId){
        return orderService.takeOrder(orderId);
    }

    @ApiOperation(value = "商家查看自己的订单")
    @GetMapping("/findData")
    public ResponseResult findData(){
        return orderService.findData();
    }

    @ApiOperation(value = "商家筛选订单")
    @PostMapping("/searchData")
    public ResponseResult searchData(
            @ApiParam(value = "订单状态",required = true)@RequestParam("orderStatus") int orderStatus,
            @ApiParam(value = "订单优先级",required = true)@RequestParam("orderPriority") int orderPriority,
            @ApiParam(value = "预约上门日期",required = true)@RequestParam("orderDate") String orderDate,
            @ApiParam(value = "上门时间点",required = true)@RequestParam("orderTime") int orderTime,
            @ApiParam(value = "顾客手机号",required = true)@RequestParam("orderClientphone") String orderClientphone
            ){
        return orderService.searchData(orderStatus,orderPriority,orderDate,orderTime,orderClientphone);
    }

    @ApiOperation(value = "默认查看的当前工程师地区未被接单的订单")
    @GetMapping("/getOrdersBy103")
    public ResponseResult getOrdersBy103(){
        return orderService.getOrdersBy103();
    }

    @ApiOperation(value = "工程师查看当前接手的但还未提交的订单")
    @GetMapping("/getOrdersNowEngineer")
    public ResponseResult getOrdersNowEngineer(){
        return orderService.getOrdersNowEngineer();
    }

    @ApiOperation(value = "工程师查看自己的所有订单")
    @GetMapping("/getOrdersFinishedEngineer")
    public ResponseResult getOrdersFinishedEngineer(){
        return orderService.getOrdersFinishedEngineer();
    }

    @ApiOperation(value = "工程师提交订单接口")
    @PostMapping("/verify")
    public ResponseResult uploadVerify(
            @ApiParam(value = "订单号",required = true)@RequestParam("orderId") Long orderId,
            @ApiParam(value = "顾客确认单",required = true)@RequestParam("orderVerify") MultipartFile orderVerify
    ){
        return orderService.uploadVerify(orderId, orderVerify);
    }

    @ApiOperation(value = "商家获取所有未被审核的订单")
    @GetMapping("/getUncheckedOrders")
    public ResponseResult getUncheckedOrders(){
        return orderService.getOrdersCheckBy102();
    }

    @ApiOperation(value = "商家审核订单接口")
    @GetMapping("/finished/{orderId}/{orderCheck}")
    public ResponseResult orderFinished(@ApiParam(value = "订单号",required = true)@PathVariable long orderId,
                                        @ApiParam(value = "审核决定(1为通过 2为拒绝)",required = true)@PathVariable int orderCheck){
        return orderService.orderFinished(orderId,orderCheck);
    }

    @ApiOperation(value = "平台最终仲裁问题订单")
    @GetMapping("/checkBy101/{orderId}/{orderCheck}")
    public ResponseResult orderCheckBy101(@ApiParam(value = "订单号",required = true)@PathVariable long orderId,
                                          @ApiParam(value = "仲裁决定(1为有问题 2为没问题)",required = true)@PathVariable int orderCheck){
        return orderService.orderCheckBy101(orderId,orderCheck);
    }

    @ApiOperation(value = "平台获取所有需要仲裁的订单")
    @GetMapping("/getOrdersBy101")
    public ResponseResult getOrdersBy101(){
        return orderService.getOrderCheckBy101();
    }

    @ApiOperation(value = "订单状态统计")
    @GetMapping("/getOrderCount/{engineerId}")
    public ResponseResult<OrderCount> getOrderCount(@ApiParam(value = "订单号",required = true)@PathVariable long engineerId) {
        return orderService.getOrderCount(engineerId);
    }

    @ApiOperation(value = "工程师退回订单申请接口")
    @PostMapping("/returnOrder")
    public ResponseResult returnOrder(@ApiParam(value = "订单号",required = true)@RequestParam("orderId") long orderId,
                                      @ApiParam(value = "退回理由",required = true)@RequestParam("reason") String reason){
        return orderService.returnOrder(orderId, reason);

    }

    @ApiOperation(value = "平台筛选查看退回订单")
    @GetMapping("/getRollbackDetailsBy101")
    public ResponseResult getOrderFormByRollbackId(@ApiParam(value = "退回订单申请状态",required = true)int status,
                                                   int page,
                                                   int limit){
        return orderService.getOrderFormByRollbackId(status, page, limit);
    }

    @ApiOperation(value = "平台处理退回订单申请")
    @PostMapping("/changeRollbackStatus/{rollbackId}")
    public ResponseResult changeRollbackStatus(@ApiParam(value = "申请ID",required = true)@PathVariable long rollbackId,
                                               @ApiParam(value = "审核决定(1通过 2拒绝)",required = true)@RequestParam int status) {
        return orderService.changeRollbackStatus(rollbackId, status);
    }

    @ApiOperation(value = "平台管理员查看状态为4（异议订单等待裁决）的订单")
    @GetMapping("/getStatus4OrderCount")
    public ResponseResult getStatus4OrderCount() {
        return orderService.getStatus4OrderCount();
    }

    @ApiOperation(value = "平台管理员查看退回订单数量")
    @GetMapping("/getOrderRollbackCount")
    public ResponseResult<OrderRollbackCount> getOrderRollbackCount() {
        return orderService.getOrderRollbackCount();
    }

    @ApiOperation(value = "商家审批订单状态统计")
    @GetMapping("/getStatusCountBy102") //查询商家所有订单
    public ResponseResult getStatusCountBy102(){
        return orderService.getStatusCountBy102();
    }

    @ApiOperation(value = "商家未审批的订单数")
    @GetMapping("/getStatus2CountBy102")
    public ResponseResult getStatus2CountBy102(){
        return orderService.getStatus2CountBy102();
    }
}
